//
//  ChangePasswordViewController.m
//  GYMWinners
//
//  Created by Martin Ma on 12-06-13.
//  Copyright (c) 2012 AJB Software Design Inc. All rights reserved.
//

#import "ChangePasswordViewController.h"

#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"
#import "SBJSON.h"
#import "MBProgressHUD.h"
#import "BaseroutAPI.h"
#import "AppDelegate.h"

@implementation ChangePasswordViewController
@synthesize oldPassword;
@synthesize newpassword;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [self setOldPassword:nil];
    [self setNewpassword:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (BOOL) validate
{
    if( [newpassword.text length] < 1)
    {
        [[BaseroutAPI sharedInstance] MessageBox:@"输入错误" Message:@"请输入密码"];
        
        return NO;
    }
    
    if( [oldPassword.text length] < 1 )
    {
        [[BaseroutAPI sharedInstance] MessageBox:@"输入错误" Message:@"请输入密码"];
        
        return NO;
    }
    
    return YES;
    
}

- (IBAction)summitChange:(id)sender {
    
    [newpassword resignFirstResponder];
    [oldPassword resignFirstResponder];
    
    if([self validate] == NO)
        return;
    
    NSMutableDictionary *slist = [[NSMutableDictionary alloc] init];
    [slist setObject:[[BaseroutAPI sharedInstance] zGetVarEx:@"customerid"] forKey:@"cid"];
    [slist setObject:newpassword.text forKey:@"newPassword"];
    [slist setObject:oldPassword.text forKey:@"oldPassword"];
    [self zSendServer:slist theTitle:@"请稍等" theCommand:@"/space/update/password"];
    
}

#pragma mark - ASIFormDataRequest delegate

- (void)requestFinished:(ASIHTTPRequest *)request
{    
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    
    if (request.responseStatusCode == 200) {
        
        NSString *responseString = [request responseString];
        NSLog(@"responseString=[%@]", responseString);   
        
        if( [[responseString lowercaseString] isEqualToString:@"success"] )
        {
            [[BaseroutAPI sharedInstance] MessageBox:@"提示" Message:@"成功"]; 
            [[self navigationController] popToRootViewControllerAnimated:YES];
        }
        else
        {
            [[BaseroutAPI sharedInstance] MessageBox:@"提示" Message:@"失败"]; 
        }
        
    } else {
        NSLog(@"Unexpected error");  
        [[BaseroutAPI sharedInstance] MessageBox:@"提示" Message:@"通讯失败"];
    }
    
}

- (void)requestFailed:(ASIHTTPRequest *)request
{    
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    
    NSError *error = [request error];
    NSLog(@"Error=%@", error.localizedDescription);   
    
    [[BaseroutAPI sharedInstance] MessageBox:@"提示" Message:@"通讯失败"];
}

@end
